//声明一个函数
let countReducer = (state=100, action) => {
  //回调函数的返回值, 将用来更新状态值
  switch(action.type){
    case 'add':
      return state + action.payload;
    case 'sub':
      return state - action.payload;
    default:
      return state;
  }
};

export default countReducer;

//同步的 action creator  action 的创造器
//封装创建 action 对象的函数   addAction(10)  => {type: 'add', payload: 10}
export let addAction = (payload) => {
  return {
    type: 'add',
    payload: payload
  }
}

//同步的 action creator 封装减少操作的 action 
export let subAction = payload => ({ type: 'sub', payload });

//异步的 action creator         async 异步的意思
export let asyncAddAction = (payload) => {
  //异步的 action 可以设置一个形参来接收 dispatch 的函数
  return (dispatch) => {
    setTimeout(() => {
      //更新状态
      dispatch({type: 'add', payload: payload})
    }, 1000);
  }
}